//
//  InlandView.m
//  ITrip
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "InlandView.h"
#import "HeaderView.h"
@implementation InlandView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createMyView];
    }
    return self;
}

- (void)createMyView
{
    NSString *path = [URLPath strategyURL];
    self.arrayData = [NSMutableArray array];
    //下载
    //创建一个AFN管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //告诉AFN不要解析
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"page" : @"1"};
    [manager GET:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayData = [StrategyInlandParser getInlandParser:operation.responseData];
        [self initTableView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    
}

- (void)initTableView
{
    //创建一个布局类
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置大小（一个方块的大小）
    layout.itemSize = CGSizeMake(self.frame.size.width / 2 - 20, 200);
    //设置最小行距
    layout.minimumLineSpacing = 10;
    //设置最小列距离
    layout.minimumInteritemSpacing = 10;
    //上下左右边距
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    
    self.myCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.frame.size.width, self.frame.size.height - 64) collectionViewLayout:layout];
    self.myCollect.delegate = self;
    self.myCollect.dataSource = self;
    
    //注册cell
    [self.myCollect registerClass:[StrategyInlandCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    // 注册标题
    [self.myCollect registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    //加背景颜色
    self.myCollect.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    self.myCollect.showsVerticalScrollIndicator = NO;
    
    [self addSubview:self.myCollect];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.frame.size.width, 30);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        HeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            headView.label.text = @"港   澳   台";
        } else if (indexPath.section == 1) {
            headView.label.text = @"内        地";
        }
        
        reusableView = headView;
    }
    return reusableView;
}



//小方块的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        NSArray *array = [self.arrayData objectAtIndex:0];
        return array.count;
    } else  {
        NSArray *array = [self.arrayData objectAtIndex:1];
//        NSLog(@"%ld", array.count);
        return array.count;
    }
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyInlandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *array = [self.arrayData objectAtIndex:indexPath.section];
    StrategyInlandModel *model = [array objectAtIndex:indexPath.row];
    [cell setStrategyModelInland:model];
    return cell;
}

//collectio的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [self.arrayData objectAtIndex:indexPath.section];
    StrategyInlandModel *model = [array objectAtIndex:indexPath.row];
    [self.Inlanddelegate sendInlandValue:indexPath.section :indexPath.row :model.inlandID : model.name_zh_cn_Inland];
}

//给collection加分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
//分区的高度


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
